﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureInstanceLevelUtils.h"
#include "IDocument.h"
#include "ILevel.h"
#include "LevelUtils.h"
#include "IInstance.h"
#include "IStructureInstanceBuildingStoreyData.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

ElementId StructureInstanceLevelUtils::GetInstanceStoreyLevelId(const IInstance* pInstance)
{
    DBG_WARN_AND_RETURN_UNLESS(pInstance, ElementId::InvalidID, L"pInstance为空",L"GDMPLab",L"2024-03-30");

    const IStructureInstanceBuildingStoreyData* pExternalData = IStructureInstanceBuildingStoreyData::Get(pInstance);
    if (!pExternalData)
        return ElementId::InvalidID;

    return pExternalData->GetBuildingStoreyId();
}

bool StructureInstanceLevelUtils::SetInstanceStoreyLevel(IInstance* pInstance, const ILevel* pStoreyLevel)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pStoreyLevel, L"pStoreyLevel为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pStoreyLevel->GetBuildingId().IsValid(), L"pStoreyLevel不是楼层",L"GDMPLab",L"2024-03-30");

    IStructureInstanceBuildingStoreyData* pExternalData = IStructureInstanceBuildingStoreyData::Get(pInstance);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pExternalData, L"请先创建楼层数据",L"GDMPLab",L"2024-03-30");

    pExternalData->SetBuildingStoreyId(pStoreyLevel->GetOwnerElement()->GetElementId());
    return true;
}

bool StructureInstanceLevelUtils::SetInstanceStoreyLevel(IDocument* pDoc, ElementId instanceId, ElementId storeyLevelId)
{
    IInstance* pInstance = dynamic_cast<IInstance*>(pDoc->GetElement(instanceId));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = LevelUtils::GetLevelById(pDoc, storeyLevelId);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pStoreyLevel, L"pStoreyLevel为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pStoreyLevel->GetBuildingId().IsValid(), L"pStoreyLevel不是楼层标高",L"GDMPLab",L"2024-03-30");

    return StructureInstanceLevelUtils::SetInstanceStoreyLevel(pInstance, pStoreyLevel);
}

bool StructureInstanceLevelUtils::CreateInstanceStoreyLevel(IInstance* pInstance, const ILevel* pStoreyLevel)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pStoreyLevel, L"pStoreyLevel为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pStoreyLevel->GetBuildingId().IsValid(), L"pStoreyLevel不是楼层",L"GDMPLab",L"2024-03-30");

    IStructureInstanceBuildingStoreyData* pExternalData = IStructureInstanceBuildingStoreyData::Get(pInstance);
    
    //TODO：楼层信息加到input后使用断言
#if 1
    if (pExternalData)
    {
        return StructureInstanceLevelUtils::SetInstanceStoreyLevel(pInstance, pStoreyLevel);
    }
#else
    DBG_WARN_AND_RETURN_FALSE_UNLESS(nullptr == pExternalData, L"已经有楼层数据了",L"GDMPLab",L"2024-03-30");
#endif

    pExternalData = IStructureInstanceBuildingStoreyData::Create(pInstance, pStoreyLevel->GetOwnerElement()->GetElementId());
    return nullptr!=pExternalData;
}

bool StructureInstanceLevelUtils::CreateInstanceStoreyLevel(IDocument* pDoc, ElementId instanceId, ElementId storeyLevelId)
{
    IInstance* pInstance = dynamic_cast<IInstance*>(pDoc->GetElement(instanceId));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = LevelUtils::GetLevelById(pDoc, storeyLevelId);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pStoreyLevel, L"pStoreyLevel为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pStoreyLevel->GetBuildingId().IsValid(), L"pStoreyLevel不是楼层标高",L"GDMPLab",L"2024-03-30");

    return StructureInstanceLevelUtils::CreateInstanceStoreyLevel(pInstance, pStoreyLevel);
}


const ILevel* StructureInstanceLevelUtils::GetWallStoreyLevelFromPositioningLevel(const IDocument* pDoc, ElementId positioningLevelId)
{
    const ILevel* pPositioningLevel = LevelUtils::GetLevelById(pDoc, positioningLevelId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositioningLevel, L"pPositioningLevel为空",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = nullptr;
    if (pPositioningLevel->GetBuildingId().IsValid())
    {
        pStoreyLevel = pPositioningLevel;
    }
    else
    {
        pStoreyLevel = GetNearestStoreyLevelFromNonStoreyLevel(pPositioningLevel);
    }

    // TODO: still not found?
    return pStoreyLevel;
}

const ILevel* StructureInstanceLevelUtils::GetBeamStoreyLevelFromPositioningLevel(const IDocument* pDoc, ElementId positioningLevelId)
{
    const ILevel* pPositioningLevel = LevelUtils::GetLevelById(pDoc, positioningLevelId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositioningLevel, L"pPositioningLevel为空",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = nullptr;
    if (pPositioningLevel->GetBuildingId().IsValid())
    {
        pStoreyLevel = GetNearestLowerStoreyLevelFromStoreyLevel(pPositioningLevel);
    }
    else
    {
        pStoreyLevel = GetNearestStoreyLevelFromNonStoreyLevel(pPositioningLevel);
    }

    // TODO: still not found?
    return pStoreyLevel;
}

const gcmp::ILevel* gcmp::StructureInstanceLevelUtils::GetColumnStoreyLevelFromPositioningLevel(const IDocument* pDoc, ElementId positioningLevelId)
{
    const ILevel* pPositioningLevel = LevelUtils::GetLevelById(pDoc, positioningLevelId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositioningLevel, L"pPositioningLevel为空",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = nullptr;
    if (pPositioningLevel->GetBuildingId().IsValid())
    {
        pStoreyLevel = pPositioningLevel;
    }
    else
    {
        pStoreyLevel = GetNearestStoreyLevelFromNonStoreyLevel(pPositioningLevel);
    }

    // TODO: still not found?
    return pStoreyLevel;
}

const ILevel* StructureInstanceLevelUtils::GetLineFamilyInstanceStoreyLevelFromPositioningLevel(const IDocument* pDoc, ElementId positioningLevelId)
{
    const ILevel* pPositioningLevel = LevelUtils::GetLevelById(pDoc, positioningLevelId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositioningLevel, L"pPositioningLevel为空",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = nullptr;
    if (pPositioningLevel->GetBuildingId().IsValid())
    {
        pStoreyLevel = pPositioningLevel;
    }
    else
    {
        pStoreyLevel = GetNearestStoreyLevelFromNonStoreyLevel(pPositioningLevel);
    }

    // TODO: still not found?
    return pStoreyLevel;
}

const ILevel* StructureInstanceLevelUtils::GetNearestStoreyLevelFromNonStoreyLevel(const ILevel* pNonStoreyLevel)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pNonStoreyLevel, L"pNonStoreyLevel为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(!pNonStoreyLevel->GetBuildingId().IsValid(), L"pNonStoreyLevel不是参照标高",L"GDMPLab",L"2024-03-30");

    const ILevel* pLowerLevel = LevelUtils::GetLowerStoreyLevel(pNonStoreyLevel->GetOwnerElement()->GetDocument(), pNonStoreyLevel->GetElevation());
    if (pLowerLevel)
    {
        return pLowerLevel;
    }
    const ILevel* pHigherLevel = LevelUtils::GetHigherStoreyLevel(pNonStoreyLevel->GetOwnerElement()->GetDocument(), pNonStoreyLevel->GetElevation());
    if (pHigherLevel)
    {
        return pHigherLevel;
    }
    return nullptr;
}

const ILevel* StructureInstanceLevelUtils::GetNearestLowerStoreyLevelFromStoreyLevel(const ILevel* pStoreyLevel)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pStoreyLevel, L"pStoreyLevel为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pStoreyLevel->GetBuildingId().IsValid(), L"pStoreyLevel不是楼层",L"GDMPLab",L"2024-03-30");

    const ILevel* pLowerLevel = LevelUtils::GetLowerStoreyLevel(pStoreyLevel->GetOwnerElement()->GetDocument(), pStoreyLevel->GetElevation(), pStoreyLevel->GetBuildingId());
    if (pLowerLevel)
    {
        return pLowerLevel;
    }
    return pStoreyLevel;
}

const gcmp::ILevel* gcmp::StructureInstanceLevelUtils::GetRaftFoundationStoreyLevelFromPositioningLevel(const IDocument* pDoc, ElementId positioningLevelId)
{
    const ILevel* pPositioningLevel = LevelUtils::GetLevelById(pDoc, positioningLevelId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositioningLevel, L"pPositioningLevel为空",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = nullptr;
    if (pPositioningLevel->GetBuildingId().IsValid())
    {
        pStoreyLevel = pPositioningLevel;
    }
    else
    {
        pStoreyLevel = GetNearestStoreyLevelFromNonStoreyLevel(pPositioningLevel);
    }

    // TODO: still not found?
    return pStoreyLevel;
}

const gcmp::ILevel* gcmp::StructureInstanceLevelUtils::GetStripFoundationStoreyLevelFromPositioningLevel(const IDocument* pDoc, ElementId positioningLevelId)
{
    return GetBeamStoreyLevelFromPositioningLevel(pDoc, positioningLevelId);
}

const gcmp::ILevel* gcmp::StructureInstanceLevelUtils::GetFloorStoreyLevelFromPositioningLevel(const IDocument* pDoc, ElementId positioningLevelId)
{
    const ILevel* pPositioningLevel = LevelUtils::GetLevelById(pDoc, positioningLevelId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositioningLevel, L"pPositioningLevel为空",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = nullptr;
    if (pPositioningLevel->GetBuildingId().IsValid())
    {
        pStoreyLevel = GetNearestLowerStoreyLevelFromStoreyLevel(pPositioningLevel);
    }
    else
    {
        pStoreyLevel = GetNearestStoreyLevelFromNonStoreyLevel(pPositioningLevel);
    }

    // TODO: still not found?
    return pStoreyLevel;
}

const gcmp::ILevel* gcmp::StructureInstanceLevelUtils::GetRoofStoreyLevelFromPositioningLevel(const IDocument* pDoc, ElementId positioningLevelId)
{
    const ILevel* pPositioningLevel = LevelUtils::GetLevelById(pDoc, positioningLevelId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPositioningLevel, L"pPositioningLevel为空",L"GDMPLab",L"2024-03-30");

    const ILevel* pStoreyLevel = nullptr;
    if (pPositioningLevel->GetBuildingId().IsValid())
    {
        pStoreyLevel = GetNearestLowerStoreyLevelFromStoreyLevel(pPositioningLevel);
    }
    else
    {
        pStoreyLevel = GetNearestStoreyLevelFromNonStoreyLevel(pPositioningLevel);
    }

    // TODO: still not found?
    return pStoreyLevel;
}
